#include "compile_run.hpp"
#include "../comm/httplib.h"
#include <stdlib.h>

using namespace ns_compile_and_run;
using namespace ns_log;
using namespace httplib;

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << " port" << std::endl; 
        exit(1);
    }

    Server svr;
    svr.Get("/hello", [](const Request &req, Response &resp){
        LOG(DEBUG) << "进入hello服务" << "\n";
        resp.set_content("hello httplib, 你好 httplib!", "text/plain;charset=utf-8");
    });

    //svr.set_base_dir("./wwwroot");
    svr.Post("/compile_and_run", [](const Request &req, Response &resp){
        std::string in_json = req.body;
        LOG(DEBUG) << "进入编译服务: \n" << in_json << "\n";
        std::string out_json;
        if (!in_json.empty())
        {
            CompileAndRun::Start(in_json, &out_json);
            LOG(DEBUG) << "out_json: \n" << out_json << "\n";

            resp.set_content(out_json, "application/json;charset=utf-8");
        }
    });
    svr.listen("0.0.0.0", atoi(argv[1]));

    
    return 0;
}

/*int main()
{
    Json::Value in_value;
    std::string in_json;

    in_value["code"] = R"(#include <iostream>
    int main()
    {
        std::cout << "你可以看见我了" << std::endl;
        int x = 0
        return 0;
    })";
    in_value["input"] = "";
    in_value["cpu_limit"] = 1;
    in_value["mem_limit"] = 10240 * 5;

    ns_json::JsonUtil::serialize(in_value, &in_json);
    std::cout << in_json << std::endl;

    std::string out_json;
    CompileAndRun::Start(in_json, &out_json);
    std::cout << out_json << std::endl;

    return 0;
}*/